const Zan = require('../../dist/index');
const { Tab, extend } = require('../../dist/index');
var app=getApp();
Page(Object.assign({}, Zan.Dialog,Tab, {
  data: {
    goodInfo:{},
    gid:"",
    sellerid:"",
    couponInfo: {},
    picWidth: wx.getSystemInfoSync().windowWidth,
    content: "",
    maxLength: 0,
    page:1,
    
    good_xq_pl: {
      list: [{
        id: 'xq',
        name: '宝贝详情',
      }, {
         id:"pl",
         name:"宝贝评论",
      }],
      selectedId: 'xq'
    },
  },
  onShareAppMessage: function () {
    return {
      title: "原价:" + this.data.goodInfo.price + "元,券后价" + this.data.goodInfo.coupon_price + "元,立省" + this.data.goodInfo.quan + "元" + this.data.goodInfo.title,
      path: '/pages/detail/detail?gid=' + this.data.gid + '&sellerid=' + this.data.sellerid+"&shop_type="+this.data.shop_type,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onShow: function () {  
  },
  onLoad: function (options) {
    //console.log(options.gid)
    this.setData({
      couponInfo: wx.getStorageSync('couponInfo'),
      gid: options.gid,
      pic_url:options.pic_url,
      shop_type:options.shop_type,
      is_kouling: app.globalData.is_kouling,
    })  
    this.getgoodinfo(); 
  },

  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    this.setData({
      [`${componentId}.selectedId`]: selectedId,
    });
    if(selectedId==="xq"){ 
      this.getgoodinfo();
    }else{
      this.getpinglun();
    }
  },
  //取商品详情
  getgoodinfo:function(){
    wx.showLoading({
      title: '加载中',
    })
   var that = this;
   //取商品详情
   wx.request({
     url: app.globalData.apiurl,
     data: {
       "a":"item",
       "id": that.data.gid,
     },
     success: function (res) {
      // console.log(res.data)
       if(res.data.data.result != null ){
        if(res.data.data.result.shop_type=="天猫"){
          res.data.data.result.shop_type="B"
        }else{
          res.data.data.result.shop_type = "C"
        }
       that.setData({
         goodInfo: res.data.data.result,
         sellerid: res.data.data.result.sellerId,
         quanid: that.getquanid(res.data.data.result.quan_url),
         
       })  
       wx.hideLoading()    
     }else{
        wx.showToast({
          title: '此商品已下架',
          duration:5000,
        })
  
        wx.navigateBack({
          
        })
     }
     }
   })
  },
 getquanid:function(u){
  var quanid=u.substring(u.indexOf("activityId=") + 11, u.indexOf("&pid"));
  if(quanid=="https://ul"){
    quanid = u.substring(u.indexOf("edetail?e=") + 10, u.indexOf("&traceId"));
  }
  return quanid;
 } ,
  
  //取评论
  getpinglun:function(){
    wx.showLoading({
      title: '加载中',
    })
    var that =this;
        wx.request({
          url: app.globalData.httpsurl,
          data: {
            "action": "getpl",
            gid: that.data.gid,
            seller_id: that.data.sellerid,
            "page": that.data.page,
          },
          method: "GET",
          success: function (res) {
           //console.log(res.data)
            if (res.data.data != null && res.data.data.length > 0) {
              that.setData({ pinglun: res.data.data, isLoading: false, isLoadover: false })
              wx.hideLoading()
            } else {
              that.setData({isLoadover: true})
              wx.hideLoading()
              wx.showToast({
                title: '暂无更多评论',
              })
            }
          
          }
        })     
  },

  //图片点击事件
  imgYu: function (event) {
    var src = event.currentTarget.dataset.src;//获取data-src
    var imgList = event.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  onReachBottom: function () {
    if (this.data.good_xq_pl.selectedId == "pl") {
      this.setData({
        isLoading: true,
        page: this.data.page + 1,
      })
      this.getpinglun()
    }
  },
  getCoupon: function (options) {
    wx.showLoading({ title: "优惠券正在生成" })
    var that = this;
    var InfoData = {}

    // 在有服务端时使用
    wx.request({
      url: app.globalData.httpsurl,
       data: {
         "action":"kouling",
         "quanid": that.data.quanid,
         "quan": that.data.goodInfo.quan,
         "gid": that.data.gid,
         "pid":app.globalData.pid,
       },
       method: "GET",
       success: function (resRequest) {
         if (resRequest.statusCode === 200 && resRequest.data.data != null) {
           wx.setClipboardData({
             data: resRequest.data.data,
             success: function (res) {
               that.setData({
                // content: resRequest.data.data.content,
                 loadingBtn: false,
                 showStatus: true,
                 maxLength: resRequest.data.data.length,
                 taokouling:"优惠券生成成功"
               })
               
             },
           }) 
           wx.hideLoading()
           that.showZanDialog({
             title: '优惠券领取成功',
             content: "亲，请打开手机淘宝开始购物吧!",
             buttons: [{
               text: '使用教程',
               type: 'explain'
             }, {
               text: '确定',
               color: '#3CC51F',
               type: 'confirm'
             }]
           }).then(({ type }) => {
             if (type === 'explain') {
                 wx.navigateTo({
                   url: "../../pages/explain/explain",
                 })
             }
             //console.log('=== dialog with custom buttons ===', `type: ${type}`);
           });
         }else{
           wx.showToast({
             title: '优惠券生成失败',
           })
         }
       }
     })
  }
}))
